/*
 * condition_variable.h
 *
 *  Created on: May 27, 2013
 *      Author: Aden
 */

#ifndef CONDITION_VARIABLE_H_
#define CONDITION_VARIABLE_H_

#include "mutex.h"
#include <pthread.h>

//this construction is to remove the reliance on pcb.h from mutex.h and condition_variable.h,
//thus removing a cyclical dependency
#ifndef PCB_STR_
#define PCB_STR_
typedef struct pcb_str PCBStr;
typedef PCBStr * PCBPtr;
#endif

typedef struct condition_str {
  pthread_mutex_t atomic;// = PTHREAD_MUTEX_INITIALIZER; //lock for "check and update" to ensure atomicity
  QueuePtr pid_queue;
  QueuePtr mutex_queue;
  
  
  char*(*wait)(struct condition_str *, struct mutex_str *, PCBPtr);
  char*(*notify)(struct condition_str *, PCBPtr, PCBPtr *, Mutex *);
} ConditionVariable;

char* condition_init( ConditionVariable *this );
char* condition_wait( ConditionVariable *this , Mutex *mutex , PCBPtr pid );
char* condition_notify( ConditionVariable *this , PCBPtr pid , PCBPtr *popped_process, Mutex *popped_mutex);


#endif /* CONDITION_VARIABLE_H_ */
